package com.antler.smt.admin.controller;

import com.antler.smt.admin.annotation.OperateCut;
import com.antler.smt.admin.mapper.AttachmentEntityMapper;
import com.antler.smt.admin.mapper.RequirementEntityMapper;
import com.antler.smt.admin.mapper.RequirementInfoMapper;
import com.antler.smt.entity.AttachmentEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.portal.RequirementFileIdReq;
import com.antler.smt.response.admin.GetImgResp;
import com.antler.smt.response.admin.GetUserListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.PictureOpService;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.FileUtil;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.DatabaseStartupValidator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 展示图片信息
 *
 * @author zhangyi
 * @date 2018/5/12 11:25
 */
@RestController
@RequestMapping("/showFile")
@Api(description = "文件展示接口")
public class FileShowController {

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Resource
    private PictureOpService pictureOpService;

    @Resource
    private RequirementInfoMapper requirementInfoMapper;

    @Resource
    private AttachmentEntityMapper attachmentEntityMapper;

    /**
     * 获取当前采购商下的图片信息
     *
     * @param id 选择获取图片id
     * @param pictureType 选择获取的图片类型
     * @return
     */
    @GetMapping("/getImg")
    @ApiOperation(value = "获取（pictureType：1 采购商，2 服务商 , 3 发布需求 4 会员图片[对应的传入相应的id值]）下的图片, 5 获取评价图片", httpMethod = "GET")
    public Result<List<GetImgResp>> getSupplierImg(@RequestParam(value = "id") long id,
                                                   @RequestParam(value = "pictureType") Integer pictureType){

        DataValidator.isBlank(String.valueOf(id),"传入的图片id不能为空！");
        DataValidator.isBlank(String.valueOf(pictureType),"传入的图片类型不能为为空!");

        Result result = null;
            result = pictureOpService.getImgInfoList(id,pictureType);
        return result;
    }

    /**
     * 发布需求文件下载
     *
     * @return
     */
    @GetMapping("/downloadFile")
    @ApiOperation(value = "文件下载", httpMethod = "GET")
    @OperateCut(content = "发布需求文件下载")
    public Result<String> downloadFile(@RequestParam(value = "requirementId") long requirementId,
                                       @RequestParam(value = "attachmentId") long attachmentId){
        DataValidator.isBlank(String.valueOf(requirementId),"发布需求id不能为空！");
        DataValidator.isBlank(String.valueOf(attachmentId),"文件id不能为空！");
        Result result = null;
            //文件传入id
            RequirementFileIdReq reFile = new RequirementFileIdReq();

            reFile.setAttachmentId(attachmentId);
            reFile.setRequirementId(requirementId);

            String fileName = requirementInfoMapper.getFileName(reFile);
            result = new Result(fileName);
        return result;
    }

    @GetMapping("/getFileInfo")
    @ApiOperation(value = "传入文件id获取文件信息",httpMethod = "GET")
    public Result<String> getFileInfo(@RequestParam(value = "id") Long id){
        Result result = null;

        DataValidator.isBlank(String.valueOf(id),"文件id不能为空！");

            AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(id);
            if(attachmentEntity != null) {
                result = new Result(attachmentEntity.getFileName());
            }else{
                result = new Result(ResultCodeEnum.APPEND_DATA_ERROR,"文件不存在！");
            }
        return result;
    }
}
